﻿using BluetoothMachine.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BluetoothMachine.ExpandHelper
{
    public static class ValueTesting
    {
        /// <summary>
        /// 检测当前时间是否是年份交换或季节交换节点
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static bool ContrastTime(this DateTime dateTime) => (dateTime.Month == 1 || dateTime.Month == 4 || dateTime.Month == 7 || dateTime.Month == 10) && dateTime.Day == 1;

        /// <summary>
        /// 计算季度===三个月为一季
        /// </summary>
        /// <param name="count">当前时间</param>
        /// <returns>1 = 1-3月；2 = 4-6月；3 = 7-9月；4 = 10-12月</returns>
        public static int GetQuarterly(this DateTime dateTime)
        {
            switch (dateTime.Month)
            {
                case int p when p >= 1 && p < 4:
                    return 1;
                case int p when p >= 4 && p < 7:
                    return 2;
                case int p when p >= 7 && p < 10:
                    return 3;
                case int p when p >= 10 && p <= 12:
                    return 4;
            }
            return 1;
        }

        /// <summary>
        /// 获取称重重量
        /// </summary>
        /// <param name="originalbyte">称重数据</param>
        /// <returns>值不正确为0</returns>
        public static double GetWeighingWeight(this byte[] originalbyte)
        {
            try
            {
                if (originalbyte != null && originalbyte.Length >= 16)
                {
                    byte[] bytes = new byte[0];
                    if (originalbyte.Length == 20) bytes = originalbyte;
                    if (originalbyte.Length < 20) return 0;
                    if (originalbyte.Length > 20)
                    {
                        var str = Encoding.Default.GetString(originalbyte).Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        if (str.Length > 3)
                        {
                            bytes = Encoding.Default.GetBytes(str[str.Length - 2]);
                        }
                        else
                        {
                            bytes = Encoding.Default.GetBytes(str[0]);
                            if (bytes.Length < 18) return 0;
                        }
                    }
                    var section1 = Encoding.Default.GetString(bytes, 0, 2);//ST
                    var section2 = Encoding.Default.GetString(bytes, 3, 2);//GS
                    var section3 = Encoding.Default.GetString(bytes, 6, 1);//+
                    var section4 = Encoding.Default.GetString(bytes, 7, 7);//value
                    double.TryParse(section4, out double w);
                    if (section1.ToUpper() == "ST" && !section3.Contains("-") && w > 100) return w;
                }
            }
            catch (Exception)
            {
                return 0;
            }
            return 0;
        }

        /// <summary>
        /// 前称重判定
        /// </summary>
        /// <param name="value">称重重量</param>
        /// <returns></returns>
        //public static (NGType, double) FormerlyKnownJudgment(this double value)
        //{
        //    var pcg = ParameterConfigurationGlobal.GetObject;
        //    double weigh = pcg.WeighBeforeFilling;
        //    double up = Math.Round(pcg.FluctuationWeighBeforeFilling + weigh, 2, MidpointRounding.AwayFromZero);
        //    double low = Math.Round(weigh - pcg.FluctuationWeighBeforeFilling, 2, MidpointRounding.AwayFromZero);
        //    return value switch
        //    {
        //        var k when k <= up && k >= low => (NGType.合格, 0),
        //        var k when k > up => (NGType.前称重偏重, value - weigh),
        //        var k when k < low => (NGType.前称重偏轻, weigh - value),
        //        _ => (NGType.错误, 0),
        //    };
        //}

        /// <summary>
        /// 后称重判定
        /// </summary>
        /// <param name="value">称重重量</param>
        /// <param name="injectionVolume">注液量</param>
        /// <returns></returns>
        //public static (NGType, double) AfterWeighingJudgment(this double value, double injectionVolume)
        //{
        //    //var pcg = ParameterConfigurationGlobal.GetObject;
        //    double weigh = pcg.WeighAfterFilling;
        //    var AfWtLow = Math.Round(weigh - pcg.FluctuationWeighAfterFilling, 2, MidpointRounding.AwayFromZero);
        //    var AfWtUp = Math.Round(weigh + pcg.FluctuationWeighAfterFilling, 2, MidpointRounding.AwayFromZero);
        //    var injUp = Math.Round(pcg.InjectionVolume + pcg.FluctuationInjectionVolume, 2, MidpointRounding.AwayFromZero);
        //    var injLow = Math.Round(pcg.InjectionVolume - pcg.FluctuationInjectionVolume, 2, MidpointRounding.AwayFromZero);
        //    #region 保留
        //    //if (injectionVolume <= injUp && injectionVolume >= injLow && value <= AfWtUp && value >= AfWtLow) return (NGType.合格, 0);
        //    //else if (injectionVolume > injUp) return ((NGType)7, injectionVolume - pcg.InjectionVolume);
        //    //else if (injectionVolume < injLow) return ((NGType)8, pcg.InjectionVolume - injectionVolume);
        //    //else if (value > AfWtUp) return ((NGType)9, value - pcg.WeighAfterFilling);
        //    //else if (value < AfWtLow) return ((NGType)10, pcg.WeighAfterFilling - value);
        //    #endregion
        //    return (value, injectionVolume) switch
        //    {
        //        var k when k.injectionVolume <= injUp && k.injectionVolume >= injLow && k.value <= AfWtUp && k.value >= AfWtLow => (NGType.合格, 0),
        //        var k when k.injectionVolume > injUp => (NGType.注液偏多, injectionVolume - pcg.InjectionVolume),
        //        var k when k.injectionVolume < AfWtLow => (NGType.注液偏少, pcg.InjectionVolume - injectionVolume),
        //        var k when k.value > AfWtUp => (NGType.总量偏重, value - pcg.WeighAfterFilling),
        //        var k when k.value < AfWtLow => (NGType.总量偏轻, pcg.WeighAfterFilling - value),
        //        _ => (NGType.错误, 0),
        //    };
        //}
    }
}
